﻿Public Class frmCustomerVeiwer

    Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
        frmCustomerAED.btnEdite.Text = "រក្សាទុក"
        frmCustomerAED.ShowDialog()
    End Sub

    Private Sub btnEdite_Click(sender As Object, e As EventArgs) Handles btnEdite.Click
        frmCustomerAED.btnEdite.Text = "កែប្រែ"
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = dgv.SelectedRows(0)
        Dim frm = New frmCustomerAED
        frm.txtId.Text = row.Cells("Customer_Id").Value
        frm.txtCus_Code.Text = row.Cells("Customer_Code").Value
        frm.txtCus_Name.Text = row.Cells("Customer_Name").Value
        frm.cboSex.Text = row.Cells("sex").Value
        frm.txtTel.Text = row.Cells("Phone").Value
        frm.txtEmail.Text = row.Cells("Email").Value
        frm.txtAdr.Text = row.Cells("isaddress").Value
        If (frm.ShowDialog = Windows.Forms.DialogResult.OK) Then
            LoadData()
        End If

    End Sub
    Public Sub LoadData()
        Cmd.Parameters("@S") = txtSearch.Text
        Me.dgv.DataSource = Cmd.ExecuteDataTable("select C.*,S.sex from tblCustomer C inner join tblsex S on S.sex_id = C.sex_id  WHERE UPPER (customer_code+customer_Name+isaddress) LIKE UPPER ('%' + @S +'%') AND C.Active = 1;")
    End Sub
    Private Sub frmCustomerVeiwer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(dgv)
        LoadData()
    End Sub

    Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
        LoadData()
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row As DataGridViewRow = Me.dgv.SelectedRows(0)
        Dim id = row.Cells("Customer_id").Value

        If MessageBox.Show("Are You Soure Want to Delete This Item?", "Delete", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If
        Cmd.Parameters("@Id") = id
        Cmd.ExecuteNonQuery("UPDATE tblCustomer SET Active = 0 WHERE Customer_Id = @Id")
        LoadData()
    End Sub
End Class
